import axios from "axios";
import qs from "qs";
import store from "../store/index";

// 定义http 对象
const http = {};
//let defaultBaseURL = "https://192.168.0.111:8080";
let defaultBaseURL = "https://ouyunc.com:8080";

// 环境的切换
if (process.env.NODE_ENV === "development") {
  axios.defaults.baseURL = defaultBaseURL;
} else if (process.env.NODE_ENV === "production") {
  axios.defaults.baseURL = defaultBaseURL;
}

//创建axios实例
const service = axios.create({
  timeout: 30000, //超时时间
});

//添加request拦截器,需要进行token的校验与拦截
service.interceptors.request.use(
  (config) => {
    return config;
  },
  (error) => {
    Promise.reject(error);
  }
);

//添加response拦截器
service.interceptors.response.use(
  (response) => {
    let res = {};
    res.status = response.status;
    res = response.data;
    return res;
  },
  (error) => {
    if (error.response && error.response.status == 404) {
      //跳转到xx页面
      console.log("404错误");
    }
    return Promise.reject(error.response);
  }
);

//封装get接口
// params={} 是设置默认值
http.get = function (url, params = {}, header) {
  params.t = new Date().getTime(); //get方法加一个时间参数,解决ie下可能缓存问题.
  return service({
    url: url,
    method: "get",
    headers: header
      ? header
      : {
        Authorization: "Bearer " + store.state.accessToken,
      },
    params,
  });
};

//封装post请求application/x-www-form-urlencoded
http.post = function (url, params = {}, data = {}, header) {
  //默认配置
  let sendObject = {
    url: url + "?" + qs.stringify(params),
    method: "post",
    headers: header
      ? header
      : {
        "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
        Authorization: "Bearer " + store.state.accessToken,
      },
    data: data,
  };
  return service(sendObject);
};

// post请求multipart/form-data
http.post_form = function (url, data = {}, header) {
  //默认配置
  let sendObject = {
    url: url,
    method: "post",
    headers: header
      ? header
      : {
        "Content-Type": "multipart/form-data;charset=UTF-8",
        Authorization: "Bearer " + store.state.accessToken,
      },
    data: data,
  };
  return service(sendObject);
};
// post请求 application/json
http.post_json = function (url, data = {}, header) {
  //默认配置
  let sendObject = {
    url: url,
    method: "post",
    headers: header
      ? header
      : {
        "Content-Type": "application/json;charset=UTF-8",
        Authorization: "Bearer " + store.state.accessToken,
      },
    data: JSON.stringify(data),
  };
  return service(sendObject);
};

//封装put方法 (resfulAPI常用)
http.put = function (url, data = {}) {
  return service({
    url: url,
    method: "put",
    headers: {
      "Content-Type": "application/json;charset=UTF-8",
      Authorization: "Bearer " + store.state.accessToken,
    },

    data: JSON.stringify(data),
  });
};

//删除方法(resfulAPI常用)
http.delete = function (url) {
  return service({
    url: url,
    method: "delete",
    headers: {
      Authorization: "Bearer " + store.state.accessToken,
    },
  });
};

//patch方法(resfulAPI常用)
http.patch = function (url) {
  return service({
    url: url,
    method: "patch",
    headers: {
      Authorization: "Bearer " + store.state.accessToken,
    },
  });
};

// 将http 对象暴露出去
export default http;
